package com.udaos.modules.train.controllers;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.subject.Subject;
import org.nutz.dao.Cnd;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Strings;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.By;
import org.nutz.mvc.annotation.Filters;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;

import com.udaos.common.base.Result;
import com.udaos.common.filter.PrivateFilter;
import com.udaos.common.page.DataTableColumn;
import com.udaos.common.page.DataTableOrder;
import com.udaos.models.school.Ud_school;
import com.udaos.models.sys.Sys_user;
import com.udaos.models.train.Ud_train_contract;
import com.udaos.models.trainer.Ud_trainer;
import com.udaos.modules.company.services.UdCompanySchoolService;
import com.udaos.modules.customer.services.UdCustomerService;
import com.udaos.modules.project.services.UdProjectService;
import com.udaos.modules.school.services.UdSchoolService;
/**
 * 培训协议
 * @author mutaotao
 *
 */
import com.udaos.modules.train.services.UdTrainContractService;
import com.udaos.modules.train.services.UdTrainlumpProductService;
import com.udaos.modules.trainer.services.UdTrainerService;
@IocBean
@At("/platform/train/contract")
@Filters({ @By(type = PrivateFilter.class)})
public class UdTrainContractController {
	
	@Inject
	private UdTrainContractService contractService;
	@Inject
	private UdCustomerService customerService;
	@Inject
	private UdProjectService projectService;
	@Inject
	private UdTrainlumpProductService trainlumpProductService;
	@Inject
	private UdTrainerService trainerService;
	@Inject
	private UdSchoolService schoolService;
	@Inject
	private UdCompanySchoolService companySchoolService;
	
	/**
	 * 首页
	 */
	@At("")
	@Ok("beetl:/platform/train/contract/index.html")
	@RequiresAuthentication
	public void index() {
		
	}
	/**
	 * 加载数据
	 * @param length
	 * @param start
	 * @param draw
	 * @param order
	 * @param columns
	 * @return
	 */
	@At
	@Ok("json:full")
	@RequiresAuthentication
	public Object data(@Param("name") String name,@Param("length") int length, @Param("start") int start, @Param("draw") int draw, @Param("::order") List<DataTableOrder> order, @Param("::columns") List<DataTableColumn> columns) {
		Cnd cnd = Cnd.NEW();
		if (!Strings.isBlank(name))
			cnd.and("name", "like", "%" + name + "%");
		//获取主体
		Subject subject = SecurityUtils.getSubject();
		Sys_user user = (Sys_user) subject.getPrincipal();
		String company_id=user.getUnit().getCompany_id();
		cnd.and("company_id", "=", company_id);
		return contractService.data(length, start, draw, order, columns, cnd, null);
	}
	
	@At("/edit/?")
	@Ok("beetl:/platform/train/contract/edit.html")
	@RequiresAuthentication
	public Object edit(String id) {
		Map<String,Object> map=new HashMap<String,Object>();
		Ud_train_contract contract=contractService.fetch(id);
		map.put("contract", contract);
		if(!Strings.isBlank(contract.getSchool_id())){
			map.put("school", schoolService.fetch(contract.getSchool_id()));
		}
		if(!Strings.isBlank(contract.getTrainer_id())){
			map.put("trainer", trainerService.fetch(contract.getTrainer_id()));
		}
		//获取驾校列表
		Subject subject = SecurityUtils.getSubject();
		Sys_user user = (Sys_user) subject.getPrincipal();
		String company_id=user.getUnit().getCompany_id();
		List<Ud_school> schoolList=companySchoolService.findSchoolById(company_id);
		map.put("schoolList", schoolList);
		
		map.put("customer", customerService.fetch(contract.getCustomer_id()));
		map.put("project", projectService.fetch(contract.getProject_id()));
		map.put("product", trainlumpProductService.fetch(contract.getProduct_id()));
		return map;
	}
	
	/**
	 * 修改
	 * @param member
	 * @param req
	 * @return
	 */
	@At
	@Ok("json")
	@RequiresAuthentication
	public Object editDo(@Param("..") Ud_train_contract contract, HttpServletRequest req) {
		try {
			String oldTrainerId=contractService.fetch(contract.getId()).getTrainer_id();
			//设置修改
			contractService.updateIgnoreNull(contract);
			if(!Strings.isBlank(contract.getTrainer_id()) && (Strings.isBlank(oldTrainerId) || !oldTrainerId.equals(contract.getTrainer_id()))){
				//发送模板消息给用户
				contractService.sendToUser(contract);
			}
	   		return Result.success("保存成功");
		} catch (Exception e) {
			return Result.error(e.getMessage());
		}
	}
	/**
	 * 获取该驾校下的培训者
	 * @param schoolId
	 * @return
	 */
	@At
	@Ok("json")
	public Object chooseTrainer(String schoolId){
		Cnd cnd=Cnd.NEW();
		cnd.and("state","=",0);
		cnd.and("school_id","=",schoolId);
		cnd.and("delFlag","=","0");
		List<Ud_trainer> list=trainerService.query(cnd);
		return list;
	}
	
	
	
	
	
	@At
	@Ok("json")
	public String getCustomer(String id){
		if(Strings.isBlank(id)){
			return "";
		}
		return customerService.fetch(id).getMobile();
	}
	@At
	@Ok("json")
	public String getProject(String id){
		if(Strings.isBlank(id)){
			return "";
		}
		return projectService.fetch(id).getName();
	}
	@At
	@Ok("json")
	public String getProduct(String id){
		if(Strings.isBlank(id)){
			return "";
		}
		return trainlumpProductService.fetch(id).getName();
	}
	@At
	@Ok("json")
	public String getSchool(String id){
		if(Strings.isBlank(id)){
			return "";
		}
		return schoolService.fetch(id).getName();
	}
	@At
	@Ok("json")
	public String getTrainer(String id){
		if(Strings.isBlank(id)){
			return "";
		}
		return trainerService.fetch(id).getName();
	}

}
